package com.yinhaochuan.query;

import java.io.BufferedReader;
import java.io.File;
import java.io.FileReader;
import java.io.IOException;
import java.util.ArrayList;
import java.util.HashSet;
import java.util.Iterator;
import java.util.List;

public class TestQuery {

	String queryFile;
	
	private String queryid;
	private String title;
	private String desc;
	private String back;
	private String rel;
	private String conc;
	
	
	public TestQuery(String queryFile){
		try {
			File  file = new File(queryFile);
			BufferedReader br = new BufferedReader(new FileReader(file));
			this.queryid =  file.getName().substring(0, file.getName().lastIndexOf("."));
			this.title = br.readLine();
			this.desc = br.readLine();
			this.back = br.readLine();
			this.rel = br.readLine();
			this.conc = br.readLine();
			br.close();
		} catch ( IOException e) {
			e.printStackTrace();
		}
	}
	
	public TestQuery(){
		
	}

	public String getQueryid() {
		return queryid;
	}

	public void setQueryid(String queryid) {
		this.queryid = queryid;
	}

	public String getTitle() {
		return title;
	}

	public void setTitle(String title) {
		this.title = title;
	}

	public String getDesc() {
		return desc;
	}

	public void setDesc(String desc) {
		this.desc = desc;
	}

	public String getBack() {
		return back;
	}

	public void setBack(String back) {
		this.back = back;
	}

	public String getRel() {
		return rel;
	}

	public void setRel(String rel) {
		this.rel = rel;
	}

	public String getConc() {
		return conc;
	}

	public void setConc(String conc) {
		this.conc = conc;
	}

	public String getQueryForOne(String query) {
		return	createQuery(query);
	}

	
	public String getQuery(){
		String query1 = getTitle();
		String query2 = getDesc();		
		return createQuery(query1, query2);
	}

	private String createQuery(String title, String desc) {
		String[] slist1 = title.split(" ");
		String[] slist2 = desc.split(" ");
		List< String> list = new ArrayList<String>();
		for (int i = 0; i < slist1.length; i++) {
			list.add(slist1[i]);
		}
		for (int i = 0; i < slist2.length; i++) {
			if (!list.contains(slist2[i])) {
				list.add(slist2[i]);
			}
		}

		StringBuffer sb = new StringBuffer();
		for (Iterator<String> iterator = list.iterator(); iterator.hasNext();) {
			String string = (String) iterator.next();
			sb.append(string);
			sb.append(" ");
		}
		
		return sb.toString();
	}

	private String createQuery(String title) {
		String[] slist = title.split(" ");
		HashSet< String> hashSet = new HashSet<String>();
		for (int i = 0; i < slist.length; i++) {
			hashSet.add(slist[i]);
		}
		
		StringBuffer sb = new StringBuffer();
		for (Iterator<String> iterator = hashSet.iterator(); iterator.hasNext();) {
			String string = (String) iterator.next();
			sb.append(string);
			sb.append(" ");
		}
		return sb.toString();
	}	
}
